<?php
/**
 *
 * 语音转写
 * @package  tp5
 * @author
 */
namespace app\api\controller;

use think\Controller;
use think\Db;
use think\Request;

class Speech1 extends Base
{
    public $maindir = "/home/wwwroot/default/data/";
    public $speech0 = "";
    public $speech1 = "";
    public $speech2 = "";
    public $userhome = "";
    public $user_id = "";
    public $temp = "";
    public function __construct()
    {
        parent::__construct();
        //$this->user_id="imsl";
        //$this->user_id = input('user_id/d', 0);
        $this->temp = $this->maindir . "temp/" . $this->user_id . "";
        $this->speech0 = $this->maindir . "speech0/" . $this->user_id . "";
        $this->speech1 = $this->maindir . "speech1/" . $this->user_id . "";
        $this->speech2 = $this->maindir . "speech2/" . $this->user_id . "";
    }

    public function detail()
    {
        $id = input('id/s');
        if (!$id) {
            return resultArray(['error' => '参数错误']);
        }
        $index = input('index/d', 0);
        $aisle = input('aisle/s', '');
        $aisle = '';

        //$recorder = Db::name('meeting_speech')->where(array('id' => $id))->find();


        // $recorder['update_time'] = isset($recorder['update_time']) ? $recorder['update_time'] : $recorder['create_time'];
        $recorder['title'] = isset($recorder['title']) ? $recorder['title'] : '未命名';
        $where['id'] = $id;
        if ($aisle && $aisle != 'null' && $aisle != '99') {
            $where['aisle'] = $aisle;
        }
        $speech = Db::name('meeting_speech')->where(array('id' => $id))->find();
        // var_dump($speech);exit;
        // var_dump($where);exit;
        $return = array();
        //$return['speech_count'] = Db::name('meeting_speech')->where($where)->count();
        // $speech = $speech[0];
        $return['audio_time'] = $speech['seconds'] ? gmstrftime('%H:%M:%S', ceil($speech['seconds']/2)) : '-';
        $lvsr = isset($speech['lvsr_data_current']) ? json_decode($speech['lvsr_data_current'], 1) : json_decode($speech['lvsr_data'], 1);
        $speech['lvsr_data'] = array();
        if (!empty($lvsr['list'])) {
            foreach ($lvsr['list'] as $l) {
                $ar = explode('-', $l['time']);
                if ($speech['create_time'] > '2017-09-10') {
                    $l['startTime'] = $ar[0] * 0.2;
                    $l['endTime'] = $ar[1] * 0.2;
                } else {
                    $l['startTime'] = $ar[0] * 0.1875;
                    $l['endTime'] = $ar[1] * 0.1875;
                }

                $l['speaker'] = isset($l['speaker']) && $l['speaker'] ? $l['speaker'] : '未标记';
                $speech['lvsr_data'][] = $l;
            }
            foreach ($speech['lvsr_data'] as $key => $row) {
                $arr2 = explode('-', $row['time']);
                $num1[$key] = $arr2[0];
            }
            array_multisort($num1, SORT_ASC, $speech['lvsr_data']);
        }
        //修改转写结果格式，按照句号分段，首行缩进。对应<br/>和&nbsp;
        $lvsr_txt = '';
        foreach ($speech['lvsr_data'] as $k => $v) {
            $lvsr_txt .= $v['txt'];
            if ($k == 0 && mb_substr($speech['lvsr_data'][$k]['txt'], 0, 4, 'utf-8') != '<br>') {
                $speech['lvsr_data'][$k]['txt'] = '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $speech['lvsr_data'][$k]['txt'];
            }
            //var_dump(mb_substr($v['txt'], -1,1, 'utf-8') == '。');
            //die;
            if (mb_substr($v['txt'], -1, 1, 'utf-8') == '。' && mb_substr($speech['lvsr_data'][$k + 1]['txt'], 0, 4, 'utf-8') != '<br>') {
                $speech['lvsr_data'][$k + 1]['txt'] = '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $speech['lvsr_data'][$k + 1]['txt'];
            }
        }

        //转写文本格式
        $return['lvsr_txt'] = $lvsr_txt;
        $return['speech_id'] = $speech['id'];
        $return['meetingId'] = $speech['meeting_id'];
        $return['lvsr_data'] = $speech['lvsr_data'];

        $return['create_time'] = date('Y-m-d', $recorder['ctime']);
        //$return['audio'] = Request::instance()->domain() . "/data/speech2/1/" . substr($speech['formatname'], 0, -3) . "mp3";
        $return['audio'] = Request::instance()->domain() . "/data/speech2/" . $speech['user_id'] . "/" . substr($speech['formatname'], 0, -3) . "mp3";
        //$recorder['wave'] = json_decode(file_get_contents("http://deepmind.raisound.com/upload/speech2/imsl/".substr($recorder['formatname'],0,-3)."json"));
        $speech_list = Db::name('meeting_speech')->where($where)->field('lvsr_status,aisle')->select();
        $aisle = array();
        foreach ($speech_list as $s) {
            if ($s['lvsr_status'] == 1) {
                $s['lvsr_status'] = '未转写';
            } else if ($s['lvsr_status'] == 2) {
                $s['lvsr_status'] = '转写中';
            } else if ($s['lvsr_status'] == 3) {
                $s['lvsr_status'] = '转写完成';
            }
            $aisle[$s['aisle']] = $s['aisle'];
            $return['speech_list'][] = $s;
        }
        //返回编辑状态
        $return['lvsr_data_edit_state'] = $speech['lvsr_data_current'] ? "已编辑" : "未编辑";
        // $recorder['aisle'] = array_values($aisle);
        $return['aisle'] = array('00', '01', '02', '03', '04', '05', '06', '07', '08');
        return resultArray(['data' => $return]);
    }

    public function download_result()
    {
        $id = input('id/s');
        if (!$id) {
            return resultArray(['error' => '参数错误']);
        }
        $index = input('index/d', 0);
        $aisle = input('aisle/s', '');
        $aisle = '';
        $recorder = Db::name('recorder')->where(array('id' => $id))->find();
        if (!$recorder) {
            return resultArray(['error' => '参数错误']);
        }
        $where['meeting_id'] = $id;
        if ($index < 99999) {
            //$where['aisle'] = $aisle;
            $speech = Db::name('meeting_speech')->where($where)->limit("$index,1")->select();
        } else {
            $speech = Db::name('meeting_speech')->where(array('meeting_id' => $id))->select();
        }
        //var_dump($speech);exit;

        foreach ($speech as $s) {
            if ($index == 99999) {
                $zip_file = "/home/wwwroot/default/data/all_" . substr($s['formatname'], 0, -3) . 'zip';
            } else {
                $zip_file = "/home/wwwroot/default/data/" . substr($s['formatname'], 0, -3) . 'zip';
            }
            // var_dump($zip_file);exit;
            $p = path_info($s['filename']);
            /* $channels = explode('_',$p['filename']);
            if($channels[1] == '08'){
            $filename = 'compound'.'_'.$channels[2];
            }else{
            $filename = 'channel_'.$channels[1].'_'.$channels[2];
            } */
            $filename = substr($s['formatname'], 0, -4);

            //continue;//exit;
            $files['mp3'][] = "/home/wwwroot/default/data/speech2/" . $s['user_id'] . "/" . substr($s['formatname'], 0, -3) . "mp3";
            $lvsr = isset($s['lvsr_data_current']) ? json_decode($s['lvsr_data_current'], 1) : json_decode($s['lvsr_data'], 1);
            $txt_content = '';
            if (!empty($lvsr['list'])) {
                foreach ($lvsr['list'] as $l) {
                    $txt_content .= $l['txt'];
                }
            }
            //var_dump($txt);exit;
            $txt = "/home/wwwroot/default/data/speech2/" . $s['user_id'] . "/" . substr($s['formatname'], 0, -3) . "txt";
            if (!file_exists($txt)) {
                file_put_contents($txt, $txt_content);
            }

            $files['names'][] = $filename;
            $files['txt'][] = $txt;
        }
        $readme = "/home/wwwroot/default/data/speech2/" . $s['user_id'] . "/" . $recorder['title'] . "-会议说明.txt";
        if (!file_exists($readme)) {
            file_put_contents($readme, '时间：' . date('Y-m-d H:i', $recorder['create_time']) . "\r\n" . "会议名称：" . $recorder['title'] . "\r\n" . "会议简介：" . $recorder['desc']);
        }
        $files['readme'][] = $readme;
        // var_dump($files);
        // exit;
        if ($index == 99999) {
            if (!file_exists($zip_file)) {
                all_compress($zip_file, $files);
                download_result_f($zip_file);
            } else {
                download_result_f($zip_file);
            }
            exit;
        } else {
            download_channel($zip_file, $files);
        }

        exit;
    }

    //保存录音修改结果
    public function edit()
    {
        $id = input('id/s');
        $result = input('lvsr_data/s');
        $info = input('info/a');
        $recorder = Db::name('meeting_speech')->where(array('id' => $id))->find();
        if (empty($recorder)) {
            return resultArray(['error' => '参数错误']);
        }
        if (!empty($info)) {
            // var_dump($info);
            $info['update_time'] = time();
            $r = Db::name('meeting_speech')->where(array('id' => $id))->update($info);
        }
        if (!empty($result)) {
            $r = Db::name('meeting_speech')->where(array('id' => $id))->update(array('lvsr_data_current' => $result, 'update_time' => time()));
        }
        if ($r) {
            return resultArray(['data' => '']);
        } else {
            return resultArray(['error' => '修改失败']);
        }
    }

    public function data()
    {
        $id = input('id/d');
        $is_initial = input('is_initial/d', 0);
        $mood_type = input('mood_type/d', 0);
        $speech = Db::name('meeting_speech')->where(array('id' => $id))->field('lvsr_data,lvsr_data_current')->find();
        if ($is_initial) {
            $lvsr_data = $speech['lvsr_data'];
        } else {
            $lvsr_data = $speech['lvsr_data_current'] ? $speech['lvsr_data_current'] : $speech['lvsr_data'];
        }
        if ($mood_type) {
            //ar_dump($lvsr_data);
            //die;
            $lvsr = fopen('/home/wwwroot/default/data/lvsr.txt', 'w');
            $lvsr_oral = fopen('/home/wwwroot/default/data/lvsr_oral.txt', 'w');
            fclose($lvsr_oral);
            fwrite($lvsr, $lvsr_data);
            system('python2 /data/disc1/asr/am-training/s8/deal_oral.py /home/wwwroot/default/data/lvsr.txt /home/wwwroot/default/data/lvsr_oral.txt');
            $lvsr_oral = fopen('/home/wwwroot/default/data/lvsr_oral.txt', 'r');
            //$lvsr_data = json_decode(file_get_contents('/home/wwwroot/default/data/lvsr_oral.txt'),1);
            $r = fread($lvsr_oral, filesize("/home/wwwroot/default/data/lvsr_oral.txt"));
            $r = str_replace(['"txt":",', '"txt":"}'], ['"txt":"",', '"txt":""}'], $r);
            $lvsr_data = json_decode(trim($r), 1);
            //var_dump($lvsr_data['list']);exit;
            //fclose($lvsr_oral);
            //echo json_encode($lvsr_data);exit;
        } else {
            $lvsr_data = json_decode($lvsr_data, 1);
        }

        // $lvsr['list'] = $lvsr_data['list'];
        if (!empty($lvsr_data['list'])) {
            foreach ($lvsr_data['list'] as $l) {
                $ar = explode('-', $l['time']);
                $l['startTime'] = $ar[0] * 0.2;
                $l['endTime'] = $ar[1] * 0.2;
                $l['speaker'] = isset($l['speaker']) && $l['speaker'] ? $l['speaker'] : '未标记';
                $return['lvsr_data'][] = $l;
            }
            foreach ($return['lvsr_data'] as $key => $row) {
                $arr2 = explode('-', $row['time']);
                $num1[$key] = $arr2[0];
            }
            array_multisort($num1, SORT_ASC, $return['lvsr_data']);
        }

        //修改转写结果格式，按照句号分段，首行缩进。对应<br/>和&nbsp;
        $return['lvsr_txt'] = '';
        foreach ($return['lvsr_data'] as $k => $v) {
            $return['lvsr_txt'] .= $v['txt'];
            if ($k == 0 && mb_substr($return['lvsr_data'][$k]['txt'], 0, 4, 'utf-8') != '<br>') {
                $return['lvsr_data'][$k]['txt'] = '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $return['lvsr_data'][$k]['txt'];
            }
            //var_dump(mb_substr($v['txt'], -1,1, 'utf-8') == '。');
            //die;
            if (mb_substr($v['txt'], -1, 1, 'utf-8') == '。' && mb_substr($return['lvsr_data'][$k + 1]['txt'], 0, 4, 'utf-8') != '<br>') {
                $return['lvsr_data'][$k + 1]['txt'] = '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $return['lvsr_data'][$k + 1]['txt'];
            }
        }


        return resultArray(['data' => $return]);
        //var_dump($lvsr_data);
    }

    /**
     * 录音文件列表
     * by zp
     * 参数
     * limit 每页数量
     * page 分页页数
     * start_time   开始时间
     * end_time 结束时间
     * match    匹配值 
     */
    public function speechFileList($start_time = "0", $end_time = "9999999999", $match = "", $limit = '10', $page = '1')
    {
        //录音文件表实例
        $rootpath = config('huisheng.file_mp3_path');
        //此处为时间模糊匹配使用
        if (!$start_time || !$end_time) {
            //有参数为空
            $start_time = 0;
            $end_time = 9999999999;
        } else if ($start_time == $end_time) {
            //同天查询，结束时间加一天
            $end_time += 86400;
        }
        try {
            //查询数据，并进行时间和模糊匹配
            $speech = Model("Speech");
            //getDateToUser返回查询数据集和总数量，最后参数为is_delete;
            $data = $speech->getDateToUser($start_time, $end_time, $match, $limit, $page);
            //查询所有文件数量
            if (!empty($data['list'])) {
                //记录状态数组
                $status_detail = ['待定', '未转写', '转写中', '已转写'];
                //初始化equipment模型
                $equipment = Model('Equipment');
                //循环遍历查询结果的文件，并求出文件大小，并对list进行格式化输出
                foreach ($data['list'] as $key => $value) {
                    //如果默认路径+用户id文件夹下有文件
                    if (file_exists($rootpath . DS . $value['user_id'] . DS . str_replace('.wav', '.mp3', $value['formatname']))) {
                        $file_size = $this->trans_byte(filesize($rootpath . DS . $value['user_id'] . DS . str_replace('.wav', '.mp3', $value['formatname'])));
                    } else {
                        //没有文件返回0
                        $file_size = "0.0KB";
                    }
                    $name = explode("_", $value['filename'])[1];
                    $data['list'][$key]['formatname'] = explode("-", $name)[0] . "-" . explode("-", $name)[2];
                    $data['list'][$key]['size'] = $file_size;
                    $data['list'][$key]['create_time'] = date("Y-m-d H:i:s", $value['lvsr_start_time']);
                    $data['list'][$key]['update_time'] = date("Y-m-d H:i:s", $value['lvsr_start_time'] + ceil($value['seconds']/2));
                    $data['list'][$key]['detail'] = $status_detail[$value['lvsr_status']];
                    $data['list'][$key]['device_name'] = $equipment->get($value['device_id'])['title'];
                    $data['list'][$key]['lvsr_data_edit_state'] = $value['lvsr_data_current'] ? "已编辑" : "未编辑";
                }
            }

            $data['totalPage'] = ceil($data['total'] / $limit);
            return  resultArray(['data' => $data]);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 获取在线转写结果
     */
    public function liveTransferData()
    {
        try {
            $id = input('id/d');
            $speech = Db::name('meeting_speech')->where(array('id' => $id))->field('filename')->find();
            //文件格式如下0001_zpzpzp-6-20190520114157_201905201143.wav
            $filename = explode("_", $speech['filename'])[1];
            if(empty($filename))
            {
                return resultArray(['error' => "文件不存在"]);     
            }
            $live = Db::name('meeting_live_speech')->where(['filename'=>$filename])->find();
            if(empty($live))
            {
                return resultArray(['error' => "文件不存在"]);     
            }
            $lvsr_data = [
                'lvsr_data' => $live['transfer'],
            ];
            return resultArray(['data' => $lvsr_data]);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 获取转写结果摘要
     * id  文件id
     */
    public function getSummary()
    {
        //检查参数
        if (empty(input('param.id'))) {
            return resultArray(['error' => '参数错误']);
        }
        $id = input('param.id');
        $num = input('param.num') ? input('param.num') : 1;
        //根据文件id，查询文件转写的文字
        $paragraph = Db::name('meeting_speech')->where(['id' => $id])->field('lvsr_data')->find();
        //将文本从json转化成数组
        $data = json_decode($paragraph['lvsr_data'], true)['list'];
        $summary = '';
        foreach ($data as $value) {
            $summary = $summary . $value['txt'];
        }
        //去除回车
        $summary = str_replace("\n", "", $summary);
        try {
            //shell命令获取摘要
            $commond = 'python /home/wwwroot/default/summary.py ' . $num . ' ' . $summary;
            exec($commond, $results);
            unset($results[0]);
            $result = '';
            for($i = 1; $i<=$num; $i++)
            {
                $result .= $i . '、' . $results[$i] . "\n";
            }         
            return resultArray(['data' => ['summary' => $result]]);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 删除文件进回收站
     * @file_id      文件id
     * @user_id     操作人id
     */
    public function fileToRecycled($id = '')
    {
        try {
            //首先拿到文件file_id和userid
            $user_id = input('param.user_id');
            if (empty($id)) {
                return resultArray(['error' => '参数不能为空']);
            }
            $speech = Model('Speech');
            $result = $speech->updateDataById(['is_delete' => 1], $id);
            if (!$result) {
                return resultArray(['error' => $speech->getError()]);
            }
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
        return resultArray(['data' => '']);
    }

    /**
     * 批量删除文件
     * @id 字符串数组
     * @delSure  全删参数 true则不管有没有id都删光全部文件
     */
    public function batchDelete($ids = [])
    {
        //获取id数组
        if (empty($ids)) {
            return resultArray(['error' => '批量删除参数为空']);
        }
        //启动事务
        try {
            $speech = Model('Speech');
            $result = $speech->deletDatas($ids);
            if($result)
            {
                return resultArray(['data' => '']);
            }
            return resultArray(['error' => '删除失败']);
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 回收站文件列表
     * @limit 分页每页数量
     * @page 分页页数
     */
    public function recycledList($limit = '10', $page = '1')
    {
        try {
            $speech = Model('Speech');
            //获取删除属性为1的speech表中的数组
            $lists = $speech->with('users')->where(['is_delete' => 1])->limit($limit)->page($page)->order('create_time desc')->select();
            $list = [];
            if ($lists) {
                $lists = collection($lists)->toArray();
                //删除的列表的数量
                $total = $speech->with('users')->where(['is_delete' => 1])->count();
                //时间处理
                foreach ($lists as $key => $value) {
                    $list[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                    $list[$key]['sfrid'] = $value['id'];
                    $list[$key]['user_id'] = $value['users']['id'];
                    $list[$key]['sid'] = $value['id'];
                    $name = explode("_", $value['filename'])[1];
                    $list[$key]['filename'] =  explode("-", $name)[0] . "-" . explode("-", $name)[2];
                }
            } else {
                $total = 0;
            }
            //vue分页需求项
            $data = [
                'list' => $list,
                'total' => $total,
                'total_page' => ceil($total / $limit)
            ];
            return resultArray(['data' => $data]);
        } catch (\Exception $e) {
            //错误返回错误信息
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 还原回收站中的文件
     * @sfrid 回收站文件id
     */
    public function recycledRestore($sfrid = '')
    {
        try {
            //首先拿到文件file_id和userid
            $user_id = input('param.user_id');
            if (empty($sfrid)) {
                return resultArray(['error' => '参数不能为空']);
            }
            $speech = Model('Speech');
            $result = $speech->updateDataById(['is_delete' => 0], $sfrid);
            if (!$result) {
                return resultArray(['error' => $speech->getError()]);
            }
        } catch (\Exception $e) {
            return resultArray(['error' => $e->getMessage()]);
        }
        return resultArray(['data' => '']);
    }

    /**
     * 批量还原回收站中的文件
     * @ids 批量还原回收站中文件的id，以,分割
     * @rtrAll 判断是否全部还原记号
     */
    public function batchRecycledRestore($ids = '', $rtrAll = false)
    {
        //启动事务
        DB::startTrans();
        try{
            //格式化字符串
            $id = explode(",", $ids);
            $speech = Model('Speech');
            if($rtrAll)
            {
                $result = $speech->where(['is_delete' => 1])->update(['is_delete' => 0]);
            }else
            {
                $result = $speech->where(['is_delete' => 1])->where('id', 'in', $id)->update(['is_delete' => 0]);;
            }
            DB::commit();
            return resultArray(['data' => '']);
        }catch(\Exception $e){
            DB::rollback();
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 删除回收站中的文件
     * @sfrid 回收站文件id
     */
    public function recycledDel($sfrid)
    {
        //启动事务
        Db::startTrans();
        try {
            $sfrid = input('param.sfrid');
            if (empty($sfrid)) {
                return resultArray(['error' => '参数错误']);
            }
            $speech = Model('Speech');
            $result = $speech->updateDataById(['is_delete' => 2], $sfrid);
            if (!$result) {
                return resultArray(['error' => $speech->getError()]);
            }
            $file = $speech->get($sfrid)->toArray();
            $path = config('huisheng.file_mp3_path') . DS . $file['user_id'] . DS . str_replace('.wav', '.mp3', $file['formatname']);
            if (is_file($path)) {
                unlink($path);
            }
            Db::commit();
            return resultArray(['data' => '']);
        } catch (\Exception $e) {
            Db::rollback();
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 批量删除回收站中的文件
     * @ids 批量删除回收站中文件的id，以,分割
     * @rtrAll 判断是否全部删除记号
     */
    public function batchRecycledDel($ids = '', $delAll = false)
    {
        //启动事务
        DB::startTrans();
        try{
            //格式化字符串
            $id = explode(",", $ids);
            $speech = Model('Speech');
            if($delAll)
            {
                $all = $speech->where(['is_delete' => 1])->select();
            }else
            {
                $all = $speech->where(['is_delete' => 1])->select($id);
            }
            foreach($all as $delete)
            {
                $folder = $delete['meeting_id'];
                $filename = str_replace(strrchr($delete->formatname, "."), ".mp3", $delete->formatname);
                $jsonname = str_replace(strrchr($delete->formatname, "."), ".json", $delete->formatname);
                //检查文件是否存在
                $filepath = config('huisheng.file_mp3_path') . '/' . $folder . '/' . $filename;
                $jsonpath = config('huisheng.file_mp3_path') . '/' . $folder . '/' . $jsonname;
                //删除操作 操作成功删除文件
                if($delete->save(['is_delete' => 2]))
                {
                    is_file($filepath) ? unlink($filepath) : '';
                    is_file($jsonpath) ? unlink($jsonpath) : '';
                }
            }
            DB::commit();
            return resultArray(['data' => '']);
        }catch(\Exception $e){
            DB::rollback();
            return resultArray(['error' => $e->getMessage()]);
        }
    }

    /**
     * 检查回收站的文件，删除超过十天以上的文件
     */
    public function timeoToDel()
    {
        if(IS_CLI)
        {
            //删除时间差值
            $time = time() - 3600*24*20;
            $speech = Model("Speech");
            $all = $speech->where(['is_delete' => 1])->where('create_time', '<', $time)->select();
            foreach($all as $delete)
            {
                $folder = $delete['meeting_id'];
                $filename = str_replace(strrchr($delete->formatname, "."), ".mp3", $delete->formatname);
                $jsonname = str_replace(strrchr($delete->formatname, "."), ".json", $delete->formatname);
                //检查文件是否存在
                $filepath = config('huisheng.file_mp3_path') . '/' . $folder . '/' . $filename;
                $jsonpath = config('huisheng.file_mp3_path') . '/' . $folder . '/' . $jsonname;
                //删除操作 操作成功删除文件
                $delete->save(['is_delete' => 2]);
                is_file($filepath) ? @unlink($filepath) : '';
                is_file($jsonpath) ? @unlink($jsonpath) : '';
            }
            return 'ok';
        }
        //记录日志
        return 'fail';
    }

    /**
     * 格式化文件名字
     * 参数
     * filename 当前上传文件名字
     */
    public function formatUploadName($filename)
    {
        //截取当前上传文件的后缀
        $ext = substr(strrchr($filename, '.'), 1);
        //格式化名字
        $formatname = trim(date('YmdHis', time()) . rand(1000, 9999) . "." . $ext);
        return $formatname;
    }

    /**
     * 储存单位转换
     * 参数
     * byte 字节数
     */
    function trans_byte($byte)
    {
        $KB = 1024;
        $MB = 1024 * $KB;
        $GB = 1024 * $MB;
        $TB = 1024 * $GB;
        if ($byte < $KB) {
            return $byte . "B";
        } elseif ($byte < $MB) {
            return round($byte / $KB, 2) . "KB";
        } elseif ($byte < $GB) {
            return round($byte / $MB, 2) . "MB";
        } elseif ($byte < $TB) {
            return round($byte / $GB, 2) . "GB";
        } else {
            return round($byte / $TB, 2) . "TB";
        }
    }
}
